<?php 
use xfyzf\api\controller\Examine;
function ALIH5URL($moshi=1,$price,$userId,$trade_no){
      $param=base64_encode(authcode($userId."\t".$trade_no."\t".$price."\t".$moshi,'ENCODE','XFALIURLH5'));
     $lianjies='https://auth.daoh.top/api/ALIURLH5.php?act=aliurlh5&url='.$param;
	$result = @get_curl($lianjies); 
    $json = json_decode($result,true);
    if($json){
        if($json['code'] == '1'){
            $alipayh5url_4 = $json['url'];
        }else{
           $alipayh5url_4 = $json['msg']; 
        }
    }else{
       $alipayh5url_4 ='获取失败'; 
    }
     return $alipayh5url_4;
}

function xfmp3($price){
       $mp = rand(1,3);
    if($mp==1){
        $mp3 = "//dict.youdao.com/dictvoice?audio=尊敬的客户，你好，你本次订单金额为".$price."元，请按照金额进行付款，否则会导致支付失败,付款后请务必返回本站否则会造成不回调&le=zh";
    }elseif($mp==2){
        $mp3= "//dict.youdao.com/dictvoice?audio=尊敬的用户，你本次交易金额为".$price."元记得不要付错了哟,付款后请务必返回本站否则会造成不回调&le=zh&keyfrom=speaker-target";
    }else{
        $mp3= "//dict.youdao.com/dictvoice?audio=请注意查看付款金额避免支付失败,付款后请务必返回本站否则会造成不回调&le=zh&keyfrom=speaker-target";
    } 
    return $mp3;
}


/**
 * 创建异步回调URL
 *
 * @param array $data 包含回调信息的关联数组
 * @return array 包含通知和返回 URL 的关联数组
 */
function creat_callback($data){//异步回调
    global $DB, $conf, $date;

    // 获取用户信息
    $userrow = safeSqlQuery("SELECT * FROM pay_user WHERE pid=:pid LIMIT 1", ['pid' => $data['pid']])['result'][0] ?? '';

    // 计算签名
    $sign = MD5("money=".$data['money']."&name=".$data['name']."&out_trade_no=".$data['out_trade_no']."&pid=".$data['pid']."&trade_no=".$data['trade_no']."&trade_status=TRADE_SUCCESS&type=".$data['type'].$userrow['key']);

    // 更新用户额度
    if ($data['status'] == 0) {
        safeSqlQuery("UPDATE `pay_user` SET `money`=`money`-:money WHERE pid=:pid", ['money' => $data['price'], 'pid' => $data['pid']], 'update');
    }

    // 更新订单状态
    safeSqlQuery("UPDATE `pay_order` SET `status` = '1', `endtime` = :endtime WHERE `trade_no` = :trade_no", ['endtime' => $date, 'trade_no' => $data['trade_no']], 'update');

    // 构建回调 URL
    $array = [
        'pid' => $data['pid'],
        'trade_no' => $data['trade_no'],
        'out_trade_no' => $data['out_trade_no'],
        'type' => $data['type'],
        'name' => $data['name'],
        'money' => $data['money'],
        'trade_status' => 'TRADE_SUCCESS'
    ];
    
    $urlstr = http_build_query($array);
    
    // 检查 notify_url 是否包含查询参数
    if (strpos($data['notify_url'], '?') !== false) {
        $url['notify'] = $data['notify_url'] . '&' . $urlstr . '&sign=' . $sign . '&sign_type=MD5';
    } else {
        $url['notify'] = $data['notify_url'] . '?' . $urlstr . '&sign=' . $sign . '&sign_type=MD5';
    }
    
    // 检查 return_url 是否包含查询参数
    if (strpos($data['return_url'], '?') !== false) {
        $url['return'] = $data['return_url'] . '&' . $urlstr . '&sign=' . $sign . '&sign_type=MD5';
    } else {
        $url['return'] = $data['return_url'] . '?' . $urlstr . '&sign=' . $sign . '&sign_type=MD5';
    }
    return $url;
}

function datatypeqrcode($srow,$QR_row,$userrow,$trade_no){
     global $conf,$xf_url,$httphost;
     
      $xf_moshi=$userrow['INPUTFREE'];
      
      switch ($srow['type']){
            case 'alipay':
                if($QR_row['hook_type'] == 0){
                     if($QR_row['cookie'] !='xfqydmfxy' && $QR_row['cookie'] !='xfqymckxy'){
                          ALIQQ_money_notify($QR_row,true);
                          $userId = getSubstr(base64_decode($QR_row['cookie']), "CLUB_ALIPAY_COM=", ";");
                          $QR_row['qr_url']= aliqrurl($userId,$trade_no,$srow['price'],$xf_moshi);
                          $data=array("qrcode"=>$xf_url."/api?url=".($QR_row['qr_url']),"money"=>$srow['price'],'url' => urldecode($QR_row['qr_url']),); 
                     }else if($QR_row['cookie'] =='xfqymckxy'){
                          $userId = $QR_row['alipayUId'];
                          xufeiqymoneymck($QR_row,$srow);
                          $QR_row['qr_url']= aliqrurl($userId,$trade_no,$srow['price'],$xf_moshi);
                          $data=array("qrcode"=>$xf_url."/api?url=".($QR_row['qr_url']),"money"=>$srow['price'],'url' =>urldecode($QR_row['qr_url']));
                     }
                    
                }else if($QR_row['hook_type'] == 1){
                    ALIQQ_money_notify($QR_row,true);
                    $userId = getSubstr(base64_decode($QR_row['cookie']), "CLUB_ALIPAY_COM=", ";");
                    $QR_row['qr_url']= aliqrurl($userId,$trade_no,$srow['price'],$xf_moshi);
                    $data=array("qrcode"=>$xf_url."/api?url=".($QR_row['qr_url']),"money"=>$srow['price'],'url' => urldecode($QR_row['qr_url']),);
                }else if($QR_row['hook_type'] == 3){
                    $data=array("qrcode"=>$xf_url."/api?url=". $QR_row['qr_url'],"money"=>$srow['price'],'url' => urldecode($QR_row['qr_url']));    
                }
               
                break;
            case 'wxpay':
                if(($QR_row['hook_type'] == 0 || $QR_row['hook_type'] == 1)){//店员或pc
                    if($QR_row['xf_type']=='5' || $QR_row['xf_type']=='4'){
            		     $data=array("qrcode"=>$QR_row['qr_url'],"money"=>$srow['price'],'url' => urldecode($QR_row['qr_url']));  
            		}else{//wxp://f2f01KIKrRwGra9ltRHxMSxgfa13Vps-hIOQ3oT7i033Mdw
            		   $data=array("qrcode"=>$xf_url."/api?url=".$QR_row['qr_url'],"money"=>$srow['price'],'url' => urldecode($QR_row['url']));
            		}
                }else if($QR_row['hook_type'] == 2){
                    if($QR_row['xf_type'] == '1'){//个码mac免输
        		          WXGM_notify($QR_row);
        		         $QR_row['qr_url']=   wxgmqrurl($QR_row,$srow['price'],$trade_no);
                		 $data=array("qrcode"=>$xf_url."/api?url=".urlencode($QR_row['qr_url']),"money"=>$srow['price'],'url' => urldecode($QR_row['qr_url']));
                    }else if($QR_row['xf_type'] == '2' || $QR_row['xf_type'] == '7'){//收款单
                           WXskd_notify($QR_row);
                           $data=array("qrcode"=>$httphost.'/Submit/img/wximg/'.$trade_no.'.png',"money"=>$srow['price'],'url' => urldecode($QR_row['qr_url']));
                    }else if($QR_row['xf_type']  == '3'){//个码不免输
                              WXGM_notify($QR_row);
                           $data=array("qrcode"=>$xf_url."/api?url=". $QR_row['qr_url'],"money"=>$srow['price'],'url' => urldecode($QR_row['qr_url']));
                    }elseif($QR_row['xf_type']  == '4'  || $QR_row['xf_type'] == '8'){//赞赏
                                 WXZSM_notify($QR_row);
                          $data=array("qrcode"=>$QR_row['qr_url'],"money"=>$srow['price'],'url' => urldecode($QR_row['qr_url']));  
                    }else if($QR_row['xf_type']  == '5' || $QR_row['xf_type']  == '9'){//经营
                             WXJYM_notify($QR_row);
                         $data=array("qrcode"=>$xf_url."/api?url=". $QR_row['qr_url'],"money"=>$srow['price'],'url' => urldecode($QR_row['qr_url']));  
                    }elseif($QR_row['xf_type']  == '6' || $QR_row['xf_type']  == '10'){//小微
                            WXXWSJ_notify($QR_row);
                        $data=array("qrcode"=>$xf_url."/api?url=". $QR_row['qr_url'],"money"=>$srow['price'],'url' => urldecode($QR_row['qr_url']));    
                    }
                }else if($QR_row['hook_type'] == 3){
                    $data=array("qrcode"=>$xf_url."/api?url=". $QR_row['qr_url'],"money"=>$srow['price'],'url' => urldecode($QR_row['qr_url']));    
                }
               break;
            case 'qqpay':
                if(($QR_row['hook_type'] == 0 || $QR_row['hook_type'] == 1)){
                    if(($QR_row['hook_type'] == 1 && $conf['aliqqpay_cron'] == 1) || $QR_row['hook_type'] == 0)QQOrder_notify($QR_row);
                    $xf_qr_row=get_curl($xf_url."/api/qqms.php?money=".$srow['price']."&beizhu=".$trade_no."&cookie=".$QR_row['cookie']."&url=".$_SERVER['HTTP_HOST']);
                    $data=array("qrcode"=>"//".$_SERVER['HTTP_HOST']."/Submit/qrcode_pay.php?ewm=".urlencode($xf_qr_row),"money"=>$srow['price'],'url' => urldecode($QR_row['qr_url']),); 
                }
                break;
            case 'usdtpay':
                    check_usdt($QR_row);
    		        $data=array("qrcode"=>$xf_url."/api?url=".urlencode($QR_row['qr_url']),'url' => urldecode($QR_row['qr_url']));
                break;
            default:
                $data=array("qrcode"=>$xf_url."/api?url=".urlencode($QR_row['qr_url']),'url' => urldecode($QR_row['qr_url']));
      }
     return $data;
  
}
function aliqrurl($userId = null, $trade_no = null, $price = null, $xf_moshi = 1) {
    global $xf_url;

    // 将参数进行编码处理
    $param = base64_encode(authcode($userId . "\t" . $trade_no . "\t" . $price . "\t" . $xf_moshi, 'ENCODE', 'XFALIURLH5'));
    
    // 构造请求链接
    $lianjies = $xf_url . '/api/ALIURLH5.php?act=aliqrurl&url=' . $param;
    
    // 发起请求并获取结果
    $result = @get_curl($lianjies);
    
    // 解析返回的 JSON 数据
    $json = json_decode($result, true);

    // 根据返回结果设置 QR_row 变量的值
    $QR_row = $json ? ($json['code'] == '1' ? $json['url'] : $json['msg']) : '获取失败';

    return $QR_row;
}
function usdt($type,$money){
    if($type!='usdtpay'){
        return $money;
    }
    $url = 'https://www.x-rates.com/calculator/?from=CNY&to=USD&amount='.$money;
    
    $res=get_curl($url);
    
    $dom = new DOMDocument();
    @$dom->loadHTML($res);
    $xpath = new DOMXPath($dom);
    $elements = $xpath->query('//span[@class="ccOutputRslt"]');
    // return $elements;
    if ($elements->length > 0) {
    // 提取汇率文本
         $rateText = $elements->item(0)->nodeValue;
        return round(floatval($rateText), 2);
    } else {
       return $money;
    }
}

function usdt_Order($address){
    $params = [
            'limit'           => 300,
            'start'           => 0,
            'direction'       => 'in',
            'relatedAddress'  => $address,
            'start_timestamp' => $start,
            'end_timestamp'   => $end,
        ];
    $ret = get_curl('https://apilist.tronscan.org/api/contract/events?address='.$address.'&start=0&limit=50');
    
    if(empty($json['data'])){
        return json_decode($ret,true);
    }
    $json = json_decode($ret,true);
    foreach ($json['data'] as $la){
        if($la['transferToAddress'] == $address ){
            $result[] = [
                'addtime'=> date("Y-m-d H:i:s",$la['timestamp'] / 1000),
                'time' => $la['timestamp'] / 1000,
                'money' => round($la['amount']/1000000,2),
                't_url' => $la['transferFromAddress']
            ];
        }
    }
    return $result;
}

?>